﻿using System.Diagnostics;
using System.ServiceProcess;

namespace ServiceEnabler
{
    public class ServiceEnabler
    {
        public static void Enable(string name)
        {
            Process cmd = new Process();
            ProcessStartInfo si = new ProcessStartInfo();
            si.FileName = "cmd.exe";
            si.Verb = "runas";
            si.Arguments = "/env /user: Administrator cmd /C sc start " + name;
            si.UseShellExecute = true;
            si.CreateNoWindow = true;

            cmd.StartInfo = si;
            cmd.Start();
        }

        public static void Disable(string name)
        {
            Process cmd = new Process();
            ProcessStartInfo si = new ProcessStartInfo();
            si.FileName = "cmd.exe";
            si.Verb = "runas";
            si.Arguments = "/env /user: Administrator cmd /C sc stop " + name;
            si.UseShellExecute = true;
            si.CreateNoWindow = true;

            cmd.StartInfo = si;
            cmd.Start();
        }

        public static bool CheckStatus(string name)
        {
            ServiceController sc = new ServiceController(name.Replace("\"",""));

            if (sc == null) return false;

            switch (sc.Status)
            {
                case ServiceControllerStatus.Running:
                    return true;
                case ServiceControllerStatus.StartPending:
                    return true;
                case ServiceControllerStatus.ContinuePending:
                    return true;
                case ServiceControllerStatus.Stopped:
                    return false;
                case ServiceControllerStatus.StopPending:
                    return false;
                default:
                    return false;
            }
        }
    }
}
